//
//  HRVisitorView.swift
//  microBlog
//
//  Created by HR_W on 16/5/20.
//  Copyright © 2016年 admin. All rights reserved.
//

import UIKit

import SnapKit

protocol HRVisitorViewDelegate: NSObjectProtocol {
    
    func userLogin()
    
    func userRegister()
}

class HRVisitorView: UIView {
    
    weak var visitorDelegate: HRVisitorViewDelegate?
    

    func updateUI(title: String, imageName: String, isHome: Bool = false) {
    
        if isHome {
            
            startAnimation()
            
        }else {
            houseView.hidden = true
            bringSubviewToFront(circleView)
        }
    
        circleView.image = UIImage(named: imageName)
        
        tipLabel.text = title
    }
    
    
    

    private func startAnimation () {
    
        let basic = CABasicAnimation(keyPath: "transform.rotation")
        
        basic.duration = 15
        
        basic.repeatCount = MAXFLOAT
        
        basic.toValue = 2 * M_PI
        
        basic.removedOnCompletion = false
        
        circleView.layer.addAnimation(basic, forKey: nil)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
       
        //startAnimation()
        
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
   private func setupUI () {
    
    //添加控件
    addSubview(circleView)
    addSubview(backView)
    addSubview(houseView)
    addSubview(tipLabel)
    addSubview(loginBtn)
    addSubview(registerBtn)
    
    //布局控件
    
    circleView.snp_makeConstraints { (make) in
        
        make.centerX.equalTo(self.snp_centerX)
        
        make.centerY.equalTo(self.snp_centerY).offset(-60)
    }
    
    houseView.snp_makeConstraints { (make) in
        
        make.center.equalTo(circleView.snp_center)
    }
    
    tipLabel.snp_makeConstraints { (make) in
        
        make.centerX.equalTo(circleView.snp_centerX)
        make.centerY.equalTo(circleView.snp_bottom).offset(16)
        // make.width.equalTo(230)
        make.height.equalTo(40)
    }
    
    loginBtn.snp_makeConstraints { (make) in
        make.left.equalTo(tipLabel.snp_left)
        make.centerY.equalTo(tipLabel.snp_bottom).offset(16)
        make.width.equalTo(80)
    }
    
    registerBtn.snp_makeConstraints { (make) in
        
        make.right.equalTo(tipLabel.snp_right)
        make.centerY.equalTo(tipLabel.snp_bottom).offset(16)
        make.width.equalTo(80)
    }
    
    backView.snp_makeConstraints { (make) in
        make.center.equalTo(circleView.snp_center)
    }
    
    let value: CGFloat = 237.0 / 255.0
    backgroundColor = UIColor(red: value, green: value, blue: value, alpha: 1)
    
    loginBtn.addTarget(self, action: #selector(loginBtnClick), forControlEvents: .TouchUpInside)
    registerBtn.addTarget(self, action: #selector(registerBtnClick), forControlEvents: .TouchUpInside)
    
}
    @objc private func loginBtnClick() {
        
        visitorDelegate?.userLogin()
        
    }
    
    @objc private func registerBtnClick() {
        
        visitorDelegate?.userRegister()
    }
    //遮盖
    private lazy var backView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //圆圈
    private lazy var circleView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //房子
    private lazy var houseView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //文案
    private lazy var tipLabel : UILabel = {
    
        let l = UILabel()
        
        l.text = "关注一些人,你将获得意想不到的惊喜,快来注册吧!"
        l.textColor = UIColor.darkGrayColor()
        
        l.preferredMaxLayoutWidth = 230
        l.font = UIFont.systemFontOfSize(14)
        l.textAlignment = .Center
        l.numberOfLines = 0
        return l
    }()
    
    //登录按钮
    private lazy var loginBtn : UIButton = {
    
        let btn = UIButton()
        
        btn.setTitle("登录", forState: .Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        let image = UIImage(named: "common_button_white_disable")
        let capW : Int = Int((image?.size.width)! * 0.5)
        let capH : Int = Int((image?.size.height)! * 0.5)
        btn.setBackgroundImage(image?.stretchableImageWithLeftCapWidth(capW, topCapHeight: capH), forState: .Normal)
        
        btn.sizeToFit()
        return btn
    }()
    
    //注册按钮
    private lazy var registerBtn : UIButton = {
    
        let btn = UIButton()
        
        btn.setTitle("注册", forState: .Normal)
        
        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        let image = UIImage(named: "common_button_white_disable")!
//        let capW : Int = Int((image?.size.width)! * 0.5)
//        let capH : Int = Int((image?.size.height)! * 0.5)
        
        let capW = Int(image.size.width * 0.5)
        let capH = Int(image.size.height * 0.5)
        btn.setBackgroundImage(image.stretchableImageWithLeftCapWidth(capW, topCapHeight: capH), forState: .Normal)
        
        btn.sizeToFit()
        
        return btn
        
    }()
    
}
